import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.plugin.filter.PlugInFilter;
import ij.process.ByteProcessor;
import ij.process.ImageProcessor;

/* 
 * This plugin thresholds all slices of a gray-level image stack (stack1)
 * and stores the result in a new image stack (stack2), which is then
 * displayed.
 */

public class Image_Stack_Demo implements PlugInFilter {
	
	ImagePlus img1;

	public int setup(String arg, ImagePlus img) {
		img1 = img;
		return DOES_8G + STACK_REQUIRED + NO_CHANGES;
	}

	public void run(ImageProcessor ignored) {
		
		// Get stack1 from the source image:
		ImageStack stack1 = img1.getImageStack();
		int w = stack1.getWidth();
		int h = stack1.getHeight();
		int n = stack1.getSize();  // stack1 has n slices

		// Create a second stack to hold the results.
		// Note that this new stack is initially empty!
		ImageStack stack2 = new ImageStack(w, h);
		
		// Iterate over all slices i = 1,...,n (!)
		for (int i = 1; i <= n; i++) {
			IJ.showStatus(i + "/" + n);
			IJ.showProgress(i, n);
			
			// Duplicate frame1 to frame2
			ImageProcessor frame1 = stack1.getProcessor(i);
			ImageProcessor frame2 = frame1.duplicate();
			
			// threshold frame2
			frame2.threshold(128);
			
			// Insert frame2 into stack2:
			stack2.addSlice(null, frame2);
		}
		// Display stack2:
		(new ImagePlus("After Thresholding", stack2)).show();
		
	}
}